<?php

namespace app\admin\controller\science;

use think\response\Json;
use think\response\View;
use app\admin\controller\Controller;

class Authentication extends Controller
{
    /**
     * @var \app\admin\service\science\AuthenticationService
     */
    protected $service = null;
    
    /**
     * 重写中间件配置
     */
    protected array $middleware = ['operLog'];
    
    public function initialize()
    {
        parent::initialize();
        $this->service = new \app\admin\service\science\AuthenticationService();
    }

    /**
     * 认证列表
     * @return Json
     * @throws \think\db\exception\DbException
     */
    public function list(): Json
    {
        return success($this->service->list());
    }

    /**
     * 认证详情
     * @param int $id
     * @return Json
     */
    public function detail(int $id): Json
    {
        return success($this->service->detail($id));
    }

    /**
     * 创建认证记录
     * @return Json
     */
    public function add(): Json
    {
        try {
            $result = $this->service->create(request());
        } catch (\Exception $e) {
            return error($e->getMessage());
        }
        return success('创建成功', $result);
    }

    /**
     * 更新认证记录
     * @return Json
     */
    public function update(): Json
    {

        $id = request()->param('id');
        try {
            $result = $this->service->update($id, request());
        } catch (\Exception $e) {
            return error($e->getMessage());
        }
        return success('更新成功', $result);
    }

    /**
     * 删除认证记录
     * @return Json
     */
    public function delete(): Json
    {
        return success($this->service->delete());
    }

    /**
     * 创建/编辑认证表单页面
     * @param int|null $id
     * @return View
     */
    public function create(int $id = null): View
    {
        $viewData = [
            'id' => $id
        ];
        return view('science/authentication/create', $viewData);
    }

    /**
     * 认证列表页面
     * @return View
     */
    public function index(): View
    {
        return view('science/authentication/index');
    }
}